import db from "../models/index.js";
import {Op} from "sequelize";
let userAddMsg = db.user_add_msg;
let userAddMsgDao = {};
//判断是否有申请
userAddMsgDao.isExisted = (_sender_account, _receiver_account, _session_id, _add_type) => {
    if(_add_type) {
        return userAddMsg.findOne({
            where: {
                _sender_account,
                _session_id,
                _add_type
            },
            raw: true,
        });
    }
    return userAddMsg.findOne({
        where: {
            _sender_account,
            _receiver_account,
            _add_type
        },
        raw: true,
    });
};
//获取我的好友和群申请列表
userAddMsgDao.getUserAddMsg = (_account) => {
    return userAddMsg.findAndCountAll({
        where: {
            [Op.or]:[
                {_sender_account:_account},
                {_receiver_account:_account}
            ]
        },
        raw: true,
    });
};
//添加好友或者群申请
userAddMsgDao.addUserAddMsg = (_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result, t) => {
    if(!t) {
        return userAddMsg.create({_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result});
    }
    return userAddMsg.create({_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result}, {transaction:t});
};
//更新好友的某条申请信息
userAddMsgDao.updateUserAddMsg = (_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result, t) => {
    if(!t) {
        return userAddMsg.update({_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result}, {where:{_session_id, _sender_account, _receiver_account}});
    }
    return userAddMsg.update({_sender_account, _receiver_account, _session_id, _send_message, _send_timestamp, _sender_remark, _add_type, _result}, {where:{_session_id, _sender_account, _receiver_account}, transaction:t});
};
//删除我的好友申请记录
userAddMsgDao.deleteUserAddMsg = (_sender_account, _receiver_account, _session_id, t) => {
    if(!t) {
        return userAddMsg.destroy({where:{_sender_account, _receiver_account, _session_id}});
    }
    return userAddMsg.destroy({where:{_sender_account, _receiver_account, _session_id}}, {transaction:t});
};
export default userAddMsgDao;
